package com.woniuxy.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.food.infra.mapper.FoodSpuMapper;
import com.woniuxy.food.infra.model.FoodSpu;
import com.woniuxy.food.infra.model.FoodTypeSpu;
import com.woniuxy.food.infra.mapper.FoodTypeSpuMapper;
import com.woniuxy.food.service.FoodTypeSpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.food.service.serviceparam.InsertFoodTypeSpuServiceParam;
import exception.MallException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

/**
 * <p>
 * 菜品类型关联菜品spu表 服务实现类
 * </p>
 *
 * @author 刘德华
 * @since 2023-07-19
 */
@Service
public class FoodTypeSpuServiceImpl extends ServiceImpl<FoodTypeSpuMapper, FoodTypeSpu> implements FoodTypeSpuService {

    @Autowired
    private FoodSpuMapper foodSpuMapper;
    @Override
    @GlobalTransactional
    public void insertFoodTypeSpu(InsertFoodTypeSpuServiceParam param) {
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("spu_id",param.getSpuId()).eq("store_id",param.getStoreId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        FoodTypeSpu foodTypeSpu = BeanUtil.toBean(param, FoodTypeSpu.class);
        save(foodTypeSpu);
    }
}
